package com.meelive.ingkee.push.platform.jpush;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.meelive.ingkee.push.InkePushHelper;
import com.meelive.ingkee.push.model.InkePushType;
import com.meelive.ingkee.push.common.PushUtils;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import static com.meelive.ingkee.push.model.Constance.TAG;

/**
 * Create by: fanfeiyue
 * on: 2019-08-23
 * desc: 3.0.7 版本之后新增的回调方式。 https://docs.jiguang.cn/jpush/client/Android/android_api/#_66
 * 新回调方式与旧的自定义Receiver兼容：
 * 配置了此Receiver以后，默认是也会发广播给旧Receiver的
 * 对于onMessage、onNotifyMessageArrived、onNotifyMessageOpened、onMultiActionClicked
 * 如果重写了这些方法，则需要调用super才会发给旧Receiver
 */
public class InkeJMsgReceiver extends JPushMessageReceiver {

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeJMsgReceiver onNotificationMessageArrived is called. " + notificationMessage.toString());
        }
        if (context == null) {
            return;
        }
        InkePushHelper.getInstance().onNotificationArrived(context, notificationMessage.notificationExtras, InkePushType.JPUSH);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeJMsgReceiver onNotificationMessageOpened is called. " + notificationMessage.toString());
        }
        if (context == null) {
            return;
        }
        InkePushHelper.getInstance().onNotificationArrived(context, notificationMessage.notificationExtras, InkePushType.JPUSH);
    }

    @Override
    public void onConnected(Context context, boolean b) {
        if (b) {
            String token = JPushInterface.getRegistrationID(context);
            InkePushHelper.getInstance().onReceiveToken(InkePushType.JPUSH, token);
        }
    }

    @Override
    public void onRegister(Context context, String s) {
        if (context == null || TextUtils.isEmpty(s)) {
            return;
        }
        InkePushHelper.getInstance().onReceiveToken(InkePushType.JPUSH, s);
    }

    /**
     * 自定义消息回调
     *
     * @param context
     * @param customMessage
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeJMsgReceiver onMessage is called. " + customMessage.toString());
        }
        if (context == null || customMessage == null || TextUtils.isEmpty(customMessage.message)) {
            return;
        }
        InkePushHelper.getInstance().onPassThroughPush(context, customMessage.message, InkePushType.JPUSH);
    }


}
